package com.organize.application.services.impl;

import com.autumn.application.service.AbstractEditApplicationService;
import com.autumn.exception.ExceptionUtils;
import com.autumn.mybatis.wrapper.EntityQueryWrapper;
import com.autumn.zero.common.library.application.services.common.CommonDataDictionaryAppService;
import com.organize.application.callback.DictionaryTypeCallback;
import com.organize.application.dto.employee.EmployeeInput;
import com.organize.application.dto.employee.EmployeeOutput;
import com.organize.application.services.EmployeeAppService;
import com.organize.entities.Department;
import com.organize.entities.Employee;
import com.organize.entities.query.EmployeeQuery;
import com.organize.repositories.DepartmentRepository;
import com.organize.repositories.EmployeeRepository;
import com.organize.repositories.query.EmployeeQueryRepository;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * </p>
 *
 * @description TODO
 * @author: 老码农
 * @create: 2020-10-17 15:05
 **/
public class EmployeeAppServiceImpl
        extends AbstractEditApplicationService<Long,
        Employee,
        EmployeeRepository,
        EmployeeQuery,
        EmployeeQueryRepository,
        EmployeeInput,
        EmployeeInput,
        EmployeeOutput,
        EmployeeOutput>
        implements EmployeeAppService {

    private final DictionaryTypeCallback dictionaryTypeCallback;

    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private CommonDataDictionaryAppService dataDictionaryAppService;

    public EmployeeAppServiceImpl(DictionaryTypeCallback dictionaryTypeCallback) {
        this.dictionaryTypeCallback = dictionaryTypeCallback;
    }

    @Override
    public String getModuleName() {
        return "职员管理";
    }

    /**
     * @param input
     * @param checkCode
     */
    private void editCheck(EmployeeInput input, boolean checkCode) {
        EntityQueryWrapper<Department> wrapper = new EntityQueryWrapper<Department>(Department.class);
        wrapper.where().eq(Department.FIELD_ID, input.getDepartmentId()).of().lockByUpdate();
        if (this.departmentRepository.countByWhere(wrapper) == 0) {
            ExceptionUtils.throwValidationException("无效的部门id。");
        }
        if (dataDictionaryAppService.getEnableDictionary(this.dictionaryTypeCallback.getPostType(), input.getPostId()) == null) {
            ExceptionUtils.throwValidationException("无效的岗位id。");
        }
        if (dataDictionaryAppService.getEnableDictionary(this.dictionaryTypeCallback.getEducationType(), input.getEducationId()) == null) {
            ExceptionUtils.throwValidationException("无效的学历id。");
        }
        if (checkCode) {
            EntityQueryWrapper<Employee> employeeWrapper = new EntityQueryWrapper<Employee>(Employee.class);
            employeeWrapper.where().eq(Employee.FIELD_CODE, input.getCode().trim()).of().lockByUpdate();
            if (this.getRepository().countByWhere(employeeWrapper) > 0) {
                ExceptionUtils.throwValidationException("职员编号[" + input.getCode() + "]已重复。");
            }
        }
    }

    @Override
    protected Employee addBefore(EmployeeInput input, EntityQueryWrapper<Employee> query) {
        this.editCheck(input, true);
        return super.addBefore(input, query);
    }

    @Override
    protected void updateBefore(EmployeeInput input, Employee entity, EntityQueryWrapper<Employee> wrapper) {
        boolean checkCode = !entity.getCode().trim().equalsIgnoreCase(input.getCode().trim());
        this.editCheck(input, checkCode);
        super.updateBefore(input, entity, wrapper);
    }
}
